<div class="wrapper wrapper-content animated page-heading fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Search</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                            <div class="row animate fadeIn">
                                <form id="searchForm" class="form-horizontal"  method="post">
                                    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Search</i></h5>
                                    </div>
                                    <div class="ibox-content">
                                        <div class="form-group"><label class="col-lg-4 control-label">Charge Code</label>
                                            <div class="col-lg-5">
                                                <input type="text" name = "s_chg_code" class="form-control">
                                            </div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-4 control-label">Charge Name</label>
                                            <div class="col-lg-5">
                                                <input type="text" name="s_chg_name" class="form-control">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-4 control-label">Compute Engine</label>
                                            <div class="col-lg-5">
                                                <select style="width: 159px;" class="chosen-select" name="s_chg_eng">
                                                    <option value=""> -- </option>
                                                    <?php foreach ($engine as $eng) {?>
                                                        <option value="<?php echo $eng->compute_engine; ?>"><?php echo $eng->meaning; ?></option>
                                                    <?php }
?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                          <label class="col-lg-4 control-label"></label>
                                              <div class="col-lg-5">
                                                  <a href="javascript:document.getElementById('searchForm').reset();" class="btn btn-warning btn-md pull-right m-t-n-xs clear-btn" id="search-form"> Clear</a>
                                                  <button id="search-btn" class="btn btn-primary btn-md pull-right m-t-n-xs search-btn btn-margin-right" > Search</button>
                                              </div>
                                          </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="row">
                                    <div class="col-lg-2">
                                        <button id = "adds" class="btn btn-success m-t-n-xs btn-md add-tab-btn" type="button"> Add</button>
                                    </div>
                                </div>
                                <div class="row" id="searchResult" >
                                    <div class="col-lg-12">
                                        <div class="ibox float-e-margins">
                                            <div class="ibox-content">
                                                <div class="table-responsive">
                                                    <table class="table table-bordered table-compact" style="width:100%;">
                                                    <thead>
                                                        <tr>
                                                            <th style="text-align:center;">Charge Code</th>
                                                            <th style="text-align:center;">Charge Name</th>
                                                            <th style="text-align:center;width:130px;">Compute Engine</th>
                                                            <th style="text-align:center;width:75px;">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody id="resultfound" >
                                                    </tbody>
                                                </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->

<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
<script>
$(document).ready(function(){
        $(".chosen-select").chosen({width:"100%;"});
        $('#adds').click(function(){
            $('#notify').text("");
                if($('#count_add').val() == 0){
                    $('#tab-list-2').append('<li id="add-link" class="links"><a id="2nd" data-toggle="tab" href="#add">Add<button id="addX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="add" class="tab-pane"><div class="panel-body"><div id="change2"></div></div></div>');
                    $("#change2").load("<?php echo $this->session->userdata('forload') . '/add_form'; ?>")
                    $('#count_add').val('1');
                    add_action('show'); search_action('close');
                }add_action('show'); search_action('close');

        });
        $("#search-btn").click(function(e) {
            e.preventDefault();
            var trHTML='';
            console.log('hi');
            $('#resultfound').empty();
            $('#searchResult').show();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/search_chg_element'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#searchForm').serialize(),
                success: function(e) {
                    console.log(e);
                    if(e == "No results found."){
                        console.log('error');
                        $('#resultfound').append("<td colspan='4' align='center'><h3>No Result Found</h3></td>");
                        notifymsg("danger","No Result Found");
                    }else{
                        console.log('success');
                        var countData=0;
                        $.each(e, function(i, item) {
                            var cb_state = "hover";
                            if(e[i].enable_flag == "Y"){
                            cb_state = "checked";
                            }
                            trHTML += '<tr style="line-height:1;padding:2px;"><input id="chg_id" type="hidden" value="'+ e[i].charge_id +'"><td style="text-align:left; padding:5px;">'+ e[i].charge_code +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].charge_name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td align="center" style="text-align:left; padding:2px;"><button  style="margin-left:5px;margin-top:2px;" class="btn btn-sm btn-outline btn-info btn-xs edit" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button style="margin-left: 2px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td></tr>';
                            countData++;
                        });
                        $('#resultfound').append(trHTML);
                        notifymsg("success","   . . . Search Success  , Found("+countData+") Item(s)");
                    }
                },
                error: function() {
                },
                complete: function() {
                    $("#search-btn").removeAttr('disabled');
                },
                beforeSend: function(){
                    $("#search-btn").attr('disabled',true);
                }
              });
            });
            $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.edit',function(){
                var id         = $(this).closest('tr').children('#chg_id').val();
                if($('#count_edit').val() == 0){
                    $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"></div></div></div>');
                    $("#count_edit").val('1');
                    }
                $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+id)
                edit_action('show'); search_action('close');
                notifymsg("hide","hide");
            });
            $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.view',function(){
                var id         = $(this).closest('tr').children('#chg_id').val();
                if($('#count_view').val() == 0){
                    $('#tab-list-2').append('<li id="view-link" class="links"><a data-toggle="tab" href="#view">View<button id="viewX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="view" class="tab-pane"><div class="panel-body"><div id="change4"></div></div></div>');
                    $("#count_view").val('1');
                    }
                $("#change4").load("<?php echo $this->session->userdata('forload') . '/view_form/'; ?>"+id)
                view_action('show'); search_action('close');
                notifymsg("hide","hide");
            });

    });
</script>